package com.example.l.zcq1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;

public class shishihuanjing extends Fragment {
    //定义最外层的表格对象
    private GridView gridView;
    //定义一个模拟数据
    private String[] title=new String[]{"空气温度","土壤湿度","CO₂浓度","光照强度",};
    //声明一个实体集合
    private ArrayList<sensor> list=new ArrayList<>();

    private MyAdpter myAdpter;
    //获取主框架的gridView控件页面并设置数据
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup,Bundle bundle){
    //获取可操作的界面并实例化对象
        View view=inflater.inflate(R.layout.shishihuanjing,gridView,false);
        //获取gridview控件
        gridView=(GridView)view.findViewById(R.id.shishihuanjing_gridview);
        //清空集合
        list.clear();
        //给集合添加数据
        list.add(new sensor("空气温度","℃",25.0));
        list.add(new sensor("土壤湿度","%",50));
        list.add(new sensor("CO₂浓度","ppm",750));
        list.add(new sensor("光照强度","Klx",15.0));
        //实例化适配器并传入数据
        myAdpter=new MyAdpter();
        gridView.setAdapter(myAdpter);
        return view;
    }

    //绑定数据的适配器
    public class MyAdpter extends BaseAdapter{
    @Override
    public  int getCount() {return list.size();}

    @Override
    public Object getItem(int position)  { return null;}

    @Override
    public long getItemId(int position) { return 0;}

    @Override
        public View getView(int position, View convertView,ViewGroup parent){
    //设置并获取子页面的视图对象
       View view=LayoutInflater.from(getActivity()).inflate(R.layout.sshj_item_view,null);
    //获取子页面当中的可以改变的标题控件
        TextView textView0=(TextView)view.findViewById(R.id.sshj_textview_item_0);
        TextView textView1=(TextView)view.findViewById(R.id.sshj_textview_item_1);
        TextView textView2=(TextView)view.findViewById(R.id.sshj_textview_item_2);
        TextView textView3=(TextView)view.findViewById(R.id.sshj_textview_item_3);
        TextView textView4=(TextView)view.findViewById(R.id.sshj_textview_item_4);

        //从集合当中获取数据并进行绑定
        String sensorName=list.get(position).getSensorName();//获取集当中sensorname当中多设置的值
        double data=list.get(position).getData();
        String dataUnit=list.get(position).getDataUnit();
        //通过值来判断显示不同颜色
        if (data==25.0){textView2.setText("25.0");}
        if (data==50){textView2.setText("50");}
        if (data==750){textView2.setText("750");}
        if (data==15.0){textView2.setText("15.0");}
        if(data>30){
            textView1.setText("报警");
            textView4.setTextColor(getResources().getColor(R.color.red));
            textView0.setBackgroundResource(R.drawable.textview_cricle_darkgray);
        }else {
            textView1.setText("正常");
            textView4.setTextColor(getResources().getColor(R.color.gray));
            textView0.setBackgroundResource(R.drawable.textview_cricle_white);
        }
        textView3.setText(list.get(position).getDataUnit());
        textView4.setText(title[position]);

        return view;


    }
    }
}
